<?php
class url extends Mod
{
	public $conn;
	public $name = 'url';
	
	public function __construct()
	{

	}
	
	public function index()
	{
		$data[] = '';
		$this->render('index', $data, $this->name);
		
	}
	
	public function table()
	{
		$data = [];
		$this->render('table', $data, $this->name);
	}
	
	
	
	public function ajax_table_info()
	{
		$tables = $this->conn->tables();
		$res = [
			'code' => 0,
			'count' => count($tables),
			'data' => $tables
		];
		die(json_encode($res));
	}
	
	public function ajax_table_detail()
	{
		$tname = Request::getParam('t_name');
		$data = $this->conn->table($tname);
		$res = [
			'code' => 0,
			'table' => $tname,
			'data' => $data,
			'count' => count($data)
		];
		die(json_encode($res));
	}
	
	public function ajax_add_field()
	{
		$data['name'] = Request::postParam('field_name');
		if (empty($data['name'])) $data['name'] = 'field_' . substr(md5(time()), 0, 6);
		$data['type'] = Request::postParam('field_type', 'text');
		$data['isrequired'] = Request::postParam('field_required', 0);
		$data['isprimary'] = Request::postParam('field_primary', 0);
		$data['default'] = Request::postParam('field_default');
		if (empty($data['default'])) $data['isdefault'] = 0;
		die(json_encode($data));
	}
	
	public function signin()
	{
		$this->render('signin');
	}
	
	public function m_signin()
	{
		$this->render('msignin');
	}
	
	public function search()
	{
		$keywords = isset($_GET['k']) ? Unit::safe($_GET['k']) : 'camelliae';
		$data['keywords'] = $keywords;
		$this->render('search', $data);
	}
	
	
	
	public function create_token()
	{
		/*while($i < 498) {
			$i ++;
			$sqlstr = "insert into user (add_time, token) values (" . time() . ",'" . rand(1001, 9998) . "')";
			$this->conn->sql = $sqlstr;
			$res = $this->conn->execute();
			var_dump($res);
		}*/
		
		
	}
	
	public function get_list()
	{
		$status = isset($_GET['s']) ? Unit::safe($_GET['s']) : -1;
		$keywords = isset($_GET['k']) ? Unit::safe($_GET['k']) : '';
		$whereStr = '';
		if ($status > -1) {
			$whereStr .= " AND `status`='$status'";
		}
		if ( ! empty($keywords)) {
			$whereStr .= " AND (`name` LIKE '%$keywords%' OR `inviter` LIKE '%$keywords%' OR `tnumber` LIKE '%$keywords%' OR `mobile` LIKE '%$keywords%' OR `token` LIKE '%$keywords%')";
		}
		$this->conn->sql = "SELECT * FROM user WHERE 1=1" . $whereStr;
		$queryData = $this->conn->rows();
		$count = $this->conn->count();
		$res = ['code' => 0, 'msg' => '返回成功', 'count' => $count];
		$res['data'] = $queryData;
		die(json_encode($res));
	}
	
	public function get_profile()
	{
		$res = ['code' => 0, 'msg' => '返回成功'];
		$token = isset($_POST['t']) ? Unit::safe($_POST['t']) : 1000;
		if (is_numeric($token) && $token > 1000 && $token < 10000) {
			$sqlstr = "SELECT * FROM user WHERE token='$token'";
			$this->conn->sql = $sqlstr;
			$queryData = $this->conn->row();
			$res['data'] = $queryData;
		}
		die(json_encode($res));
	}
	
	public function save()
	{
		$res = ['code' => 0, 'msg' => '返回成功'];
		$id = $_POST['id'];
		$field = $_POST['field'];
		$value = $_POST['value'];
		$sets = ",edit_time='" . time() . "'";
		if ($field == 'status' && $value == 1) {
			$sets .= ",confirm_time='" . time() . "'";
		}
		$sqlstr = "UPDATE user SET $field='$value'" . $sets . " WHERE id=$id";
		//$res['sql'] = $sqlstr;
		$this->conn->sql = $sqlstr;
		$exec = $this->conn->execute();
		$res['status'] = $exec['code'];
		$res['msg'] = $exec['msg'];
		die(json_encode($res));
	}
	
}